package com.intct.util;

import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author : Felix
 * @Date : 2025.05.16 11:17
 * TODO :
 **/
public class RedisUtil {
    private static JedisPool jedisPool;

    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(1000);
        poolConfig.setMaxIdle(100);
        poolConfig.setMinIdle(50);

        String host = ParmUtil.getValue("redis-host", "cdh-node");
        String port = ParmUtil.getValue("redis-port", "6379");
        String passwd = ParmUtil.getValue("redis-password", "");

        if (passwd != null && !StringUtils.isEmpty(passwd)) {
            // 使用Jedis的连接池
            jedisPool = new JedisPool(poolConfig, host, Integer.valueOf(port), 5000, passwd);
        } else {
            jedisPool = new JedisPool(poolConfig, host, Integer.valueOf(port), 5000);
        }
    }

    public static Jedis getConnection() {
        return jedisPool.getResource();
    }

    public static void closeConnection(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}
